import time
import requests
from importlib import import_module

from settings import PROXIES_INTERFACE, GET_ALL_PROXY_HANDLER, ID, GET_NUMBER


class URLNotStandard(Exception):
    pass


class ClassNotFoundError(Exception):
    pass


class ProcessSettings(object):

    def process(self):
        info = GET_ALL_PROXY_HANDLER.rsplit('.', 1)
        try:
            module = import_module(info[0])
        except:
            raise ModuleNotFoundError('模块{}未找到'.format(info[0]))

        cls = info[1]
        if hasattr(module, cls):
            cls = getattr(module, cls)
            o = cls()
            return list(o.handler())
        else:
            raise ClassNotFoundError('类{}在模块{}下未找到'.format(cls, info[0]))


process_obj = ProcessSettings()


class BaseHandler(object):

    def handler(self):
        raise NotImplementedError()


class GetAllProxyFromURL(BaseHandler):

    def handler(self):
        response = requests.get(PROXIES_INTERFACE)
        ip_pool_list = response.json()['data']
        return [{'proxy': ip_dict['ip'], 'expiration': ip_dict['expire_at']} for ip_dict in ip_pool_list]



